package com.derek.omshopping.ui.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.derek.omshopping.R;

import java.io.File;

public class PrivatePersonActivity extends AppCompatActivity {
    private ImageView person_back, header_image;
    private LinearLayout person_layout1, person_layout2, person_layout3,
            person_layout4, person_layout5, person_layout6;
    private PopupWindow popupWindow;
    private View pview;
    private Uri imageuri;//图片的uri

    /* 头像文件 */
    private static final String IMAGE_FILE_NAME = "temp_head_image.jpg";

    /**
     *  请求识别码
     *
     *  */
    private static final int CODE_GALLERY_REQUEST = 0xa0;
    //
    private static final int CODE_CAMERA_REQUEST = 0xa1;
    //选取相册图片设置图片
    private static final int CODE_RESULT_REQUEST = 0xa2;

    // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。
    private static int output_X = 480;
    private static int output_Y = 480;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_private_person);

        person_back = (ImageView) findViewById(R.id.person_back);
        header_image = (ImageView) findViewById(R.id.header_image);
        person_layout1 = (LinearLayout) findViewById(R.id.person_layout1);
        person_layout2 = (LinearLayout) findViewById(R.id.person_layout2);
        person_layout3 = (LinearLayout) findViewById(R.id.person_layout3);
        person_layout4 = (LinearLayout) findViewById(R.id.person_layout4);
        person_layout5 = (LinearLayout) findViewById(R.id.person_layout5);
        person_layout6 = (LinearLayout) findViewById(R.id.person_layout6);

        pview = LayoutInflater.from(PrivatePersonActivity.this).inflate(R.layout.person_pop_layout, null);
        popupWindow = new PopupWindow(pview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);


        //返回
        person_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PrivatePersonActivity.this.finish();
            }
        });

        //更换头像
        person_layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //显示弹出窗口位置
                popupWindow.showAtLocation(pview, Gravity.BOTTOM, 0, 0);

            }
        });
    }

    public void Click(View view) {

        switch (view.getId()) {

            case R.id.photo_tv:

                /**
                 * 调用系统相机
                 */
                choseHeadImageFromCameraCapture();


                break;
            case R.id.DCIM_tv:

                //从相册中选取
                popupWindow.dismiss();
                choseHeadImageFromGallery();

                break;
            case R.id.Cancel_tv:

                //取消
                popupWindow.dismiss();
                break;
        }

    }

    /**
     * 拍照裁剪重写的方法
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 用户没有进行有效的设置操作，返回
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show();
            return;
        }
        switch (requestCode) {
            case CODE_GALLERY_REQUEST:
                cropRawPhoto(data.getData());
                break;

            case CODE_CAMERA_REQUEST:
                if (hasSdcard()) {
                    File tempFile = new File(
                            Environment.getExternalStorageDirectory(),
                            IMAGE_FILE_NAME);
                    cropRawPhoto(Uri.fromFile(tempFile));
                } else {
                    Toast.makeText(getApplication(), "没有SDCard!", Toast.LENGTH_LONG)
                            .show();
                }

                break;

            case CODE_RESULT_REQUEST:
                if (data != null) {
                    setImageToHeadView(data);
                }

                break;
        }


    }

    // 从本地相册选取图片作为头像
    private void choseHeadImageFromGallery() {
        Intent intentFromGallery = new Intent();
        // 设置文件类型
        intentFromGallery.setType("image/*");
        intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST);
    }

    // 启动手机相机拍摄照片作为头像
    private void choseHeadImageFromCameraCapture() {

        Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // 判断存储卡是否可用，存储照片文件
        if (hasSdcard()) {
            intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                    .fromFile(new File(Environment
                            .getExternalStorageDirectory(), IMAGE_FILE_NAME)));
        }

        startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);
    }

    /**
     * 检查设备是否存在SDCard的工具方法
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 有存储的SDCard
            return true;
        } else {
            return false;
        }
    }

    /**
     * 提取保存裁剪之后的图片数据，并设置头像部分的View
     */
    private void setImageToHeadView(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            header_image.setImageBitmap(photo);
        }
    }

    /**
     * 裁剪原始的图片
     */
    public void cropRawPhoto(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");

        // 设置裁剪
        intent.putExtra("crop", "true");

        // aspectX , aspectY :宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX , outputY : 裁剪图片宽高
        intent.putExtra("outputX", output_X);
        intent.putExtra("outputY", output_Y);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, CODE_RESULT_REQUEST);
    }

}